﻿(function () {
    $layer = window.$layer = function (options) {
        return new $layer.prototype().o.init(options);
    };
    $layer.prototype = function () {
        this.o = {
            config: {
                version: "1.0", name: "", id: "", type: 0, zclass: "layer",
                oclass: { head: "layer-head", body: "layer-body", foot: "layer-foot", bodycontent: { content: "msg", icon: "icon" }, closebtn: "closebtn", footbtn: "btn" },
                oframe: { head: true, body: true, foot: true, headbtn: { close: true} },
                title: "no title", maxmin: true, offset: { left: "auto", top: "auto" },
                area: { width: 550, height: 370 }, border: { show: true, width: 1, color: "#cccccc", rgba: 0.4 }, shade: { show: true, color: "#000000", rgba: 0.1, clickClose: false }, closeBtn: { show: true, icon: null }, timeout: 0, fix: false,
                move: { ismove: true, out: false, obj: null, type: false }, bgcolor: "#000000", zIndex: 999999, maxWidth: 1000,
                fadeIn: { isfadeIn: true, time: 300 },
                btns: { btn: ["确定", "取消"], btnclass: ["yes", "no"], count: 2 }, dialog: { icon: null, msg: "", success: null }, iframe: { src: "", scrolling: "auto", success: null },
                page: { html: "" }, loading: { icon: null }, layerdemo: null, borderHTML: "", shadeHTML: "", topHTML: "", success: null, endsuccess: null, err: "no err", stop: false, obj: null
            },
            extend: function (NewObject, OldObject) {
                function clonePrototype() { }
                clonePrototype.prototype = NewObject;
                var obj = new clonePrototype();
                for (var ele in obj) {
                    if (typeof (obj[ele]) == "object")
                        this.extend(obj[ele], OldObject[ele]);
                    else
                        OldObject[ele] = obj[ele];
                }
            },
            init: function (parameter) {
                if (this.config.type == 1 || this.config.type == 2) {
                    this.config.oframe.foot = false;
                }
                if (parameter)
                { this.extend(parameter, this.config); }
                if (this.config.offset.left == "auto") {
                    this.config.offset.left = (parseInt(document.documentElement.clientWidth) - this.config.area.width) / 2;
                    this.config.offset.top = (parseInt(document.documentElement.clientHeight) - this.config.area.height) / 2;
                }
                var NewDataTime = new Date();
                this.config.id = "layer" + NewDataTime.getTime();
                var maxzindex = 999999;
                $(".layer").each(function () {
                    if (parseInt($(this).css("z-index")) > maxzindex) {
                        maxzindex = parseInt($(this).css("z-index"));
                    }
                });
                if (maxzindex != 999999) {
                    maxzindex += 2;
                }
                this.config.zIndex = maxzindex;
                this.instance();
                this.shade();
                document.documentElement.appendChild(this.config.obj);
                return this;
                
                //alert(this.parseHTML(this.config.obj));
            },
            instance: function () {
                this.setlayerobject();
                this.setlayerStyle();
                this.bindevent();
            },
            bindevent: function () {
                if (this.config.move.ismove) {
                    if (this.config.move.obj) {
                        new Drag(this.config.move.obj, this.config.obj, { opacity: 10, keepOrigin: this.config.move.type });
                    }
                    else {
                        new Drag(this.$("." + this.config.oclass.head, this.config.obj), this.config.obj, { opacity: 10, keepOrigin: this.config.move.type });
                    }
                }
                var closebtn = this.$("." + this.config.oclass.closebtn, this.config.obj);
                if (closebtn) {
                    this.bind(this.$("." + this.config.oclass.closebtn, this.config.obj), "click", this.close, this);
                }
                //if (this.config.dialog.success) {
                //    this.bind(this.$("." + this.config.oclass.closebtn, this.config.obj), "click", this.config.dialog.success, this);
                //}
            },
            setlayerStyle: function () {
                var csstext = "width:" + this.config.area.width + "px;";
                csstext += "height:" + this.config.area.height + "px;";
                csstext += "z-index:" + this.config.zIndex + ";";
                csstext += "position:" + (this.config.fix ? "fixed" : "absolute") + ";";
                csstext += "top:" + this.config.offset.top + "px;left:" + this.config.offset.left + "px;";
                if (this.config.border.show) {
                    csstext += "border:" + this.config.border.width + "px solid " + this.config.border.color + ";";
                    csstext += "border:" + this.config.border.width + "px solid " + this.colorRgb(this.config.border.color, this.config.border.rgba) + ";";
                }
                this.config.obj.style.cssText = csstext;
            },
            setlayerobject: function () {
                if (!this.config.layerdemo) {
                    var strHTML = "<div id=\"" + this.config.id + "\" class=\""+this.config.zclass+"\"><div class=\"layer-head\"><div class=\"title\">" + this.config.title + "</div></div><div class=\"layer-body\"></div><div class=\"layer-foot\"></div></div>";
                    this.config.obj = this.parseNode(strHTML);
                    if (this.config.oframe.headbtn.close) {
                        this.$("." + this.config.oclass.head, this.config.obj).appendChild(this.parseNode("<div class=\"" + this.config.oclass.closebtn + "\" title=\"关闭\">×</div>"));
                    }
                }
                else {
                    this.config.obj = this.parseNode(this.config.layerdemo);
                    var layerbody = this.$("." + this.config.oclass.body, this.config.obj);
                    if (!layerbody) {
                        alert("自定义UI中未定义包含 class=" + this.config.oclass.body + "元素，layer将无法创建");
                    }
                }
                if (!this.config.oframe.head) {
                    this.config.obj.removeChild(this.$("." + this.config.oclass.head, this.config.obj));
                    this.$("." + this.config.oclass.body, this.config.obj).style.top = 0;
                    this.$("." + this.config.oclass.body, this.config.obj).style.backgroundColor = "transparent";
                }
                if (!this.config.oframe.foot) {
                    this.config.obj.removeChild(this.$("." + this.config.oclass.foot, this.config.obj));
                    this.$("." + this.config.oclass.body, this.config.obj).style.bottom = 0;
                }
                switch (this.config.type) {
                    case 0: //alert
                        if (!this.config.layerdemo) {
                            var okbtn = this.parseNode("<b><button class=\"btn-ok " + this.config.oclass.footbtn + "\">确定</button></b>");
                            this.$("." + this.config.oclass.foot, this.config.obj).appendChild(okbtn);
                            this.bind(okbtn, "click", this.close);
                        }
                        this.$("." + this.config.oclass.body, this.config.obj).appendChild(this.parseNode("<div class=\"" + this.config.oclass.bodycontent.icon + "\"><div class=\"icon-" + this.config.dialog.icon + "\"></div></div>"));
                        this.$("." + this.config.oclass.body, this.config.obj).appendChild(this.parseNode("<div class=\"" + this.config.oclass.bodycontent.content + "\">" + this.config.dialog.msg + "</div>"));
                        break;
                    case 1: //confirm
                        if (!this.config.layerdemo) {
                            for (var i = 0; i < this.config.btns.count; i++) {
                                if (this.config.btns.btn.length > i) {
                                    var btn = this.parseNode("<b><button class=\"btn-ok " + this.config.oclass.footbtn + "\">确定</button></b>");
                                    if (i == 1) {
                                        btn = this.parseNode("<b><button class=\"btn-no " + this.config.oclass.footbtn + "\">取消</button></b>");
                                    }
                                    else {
                                        if (this.config.dialog.success) {
                                            this.bind(btn, "click", this.config.dialog.success, this);
                                        }
                                    }
                                    this.$("." + this.config.oclass.foot, this.config.obj).appendChild(btn);
                                    this.bind(btn, "click", this.close);
                                }
                                else {
                                    break;
                                }
                            }
                            this.$("." + this.config.oclass.body, this.config.obj).appendChild(this.parseNode("<div class=\"" + this.config.oclass.bodycontent.icon + "\"><div class=\"icon-" + this.config.dialog.icon + "\"></div></div>"));
                            this.$("." + this.config.oclass.body, this.config.obj).appendChild(this.parseNode("<div class=\"" + this.config.oclass.bodycontent.content + "\">" + this.config.dialog.msg + "</div>"));
                        }
                        break;
                    case 2: //loding
                        this.$("." + this.config.oclass.body, this.config.obj).appendChild(this.parseNode("<div class=\"loading-" + this.config.dialog.icon + "\"></div>"));
                        break;
                    case 3: //page
                        if (!this.config.layerdemo) {
                            this.$("." + this.config.oclass.body, this.config.obj).innerHTML = this.config.page.html;
                        }
                        break;
                    case 4:
                        if (!this.config.layerdemo) {
                            for (var i = 0; i < this.config.btns.count; i++) {
                                if (this.config.btns.btn.length > i) {
                                    var btn = this.parseNode("<b><button id=\"layer-btn-ok-" + this.config.id + "\" class=\"btn-ok " + this.config.oclass.footbtn + "\">确定</button></b>");
                                    if (i == 1) {
                                        btn = this.parseNode("<b><button class=\"btn-no " + this.config.oclass.footbtn + "\">取消</button></b>");
                                        this.bind(btn, "click", this.close);
                                    }
                                    else {
                                        if (this.config.dialog.success) {
                                            this.bind(btn, "click", this.config.dialog.success, this);
                                        }
                                        // this.bind(btn, "click", top.window["layerwinfuc" + this.config.id], this);
                                    }
                                    this.$("." + this.config.oclass.foot, this.config.obj).appendChild(btn);
                                }
                                else {
                                    break;
                                }
                            }
                            if (this.config.iframe.success) {
                                window["layergofunc" + this.config.id] = this.config.iframe.success;
                            }
                            if (this.config.iframe.src.indexOf("?") != -1) {
                                this.config.iframe.src += "&layer-id=" + this.config.id;
                            }
                            else {
                                this.config.iframe.src += "?layer-id=" + this.config.id;
                            }
                            this.$("." + this.config.oclass.body, this.config.obj).innerHTML = "<iframe src=\"" + this.config.iframe.src + "\" frameborder=\"0\" style=\"width:100%;height:100%;border:none;margin:0; padding:0;\"></iframe>"; ;
                        }
                        break;
                    case 5:
                        alert("要创建的modal类型不明确！"); this.config.stop = true;
                        break;
                }
            },
            show: function () {

                //document.documentElement.
            },
            shade: function () {
                if (this.config.shade.show) {
                    var cssString = "z-index:" + (this.config.zIndex - 1) + ";background-color:" + this.config.shade.color + ";opacity:" + this.config.shade.rgba + ";filter: alpha(opacity=" + (parseFloat(this.config.shade.rgba) * 100) + ");";
                    var Shadenode = this.parseNode("<div id=\"" + this.config.id + "-shade\" class=\"layer-shade\" style=\"" + cssString + "\"></div>");
                    document.documentElement.appendChild(Shadenode);
                    if (this.config.shade.clickClose == true) {
                        this.bind(Shadenode, "click", this.close);
                    }
                }
            },
            bind: function (object, type, fn) {
                var temparguments = [].slice.apply(arguments);
                var temparry = [];
                if (temparguments.length > 3) {
                    temparry = temparguments.slice(3, temparguments.length);
                }
                if (object.attachEvent) {
                    object.attachEvent("on" + type, (function () {
                        return function (event) {
                            window.event.cancelBubble = true;
                            object.attachEvent = [fn.apply(object, temparry)];
                        }
                    })(object), false);
                } else if (object.addEventListener) {
                    object.addEventListener(type, function (event) {
                        event.stopPropagation();
                        fn.apply(this, temparry)
                    });
                }
            },
            colorRgb: function (color, tmValue) {
                var sColor = color.toLowerCase();
                var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
                if (sColor && reg.test(sColor)) {
                    if (sColor.length === 4) {
                        var sColorNew = "#";
                        for (var i = 1; i < 4; i += 1) {
                            sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
                        }
                        sColor = sColorNew;
                    }
                    var sColorChange = [];
                    for (var i = 1; i < 7; i += 2) {
                        sColorChange.push(parseInt("0x" + sColor.slice(i, i + 2)));
                    }
                    return "rgba(" + sColorChange.join(",") + "," + tmValue + ")";
                } else {
                    return sColor;
                }
            },
            $: function (q, o) {
                if (typeof (q) !== 'string' || q == '') { return []; }
                var ss = q.split(' '); var attr = ''; var s = ss[0].split(':')[0];
                if (s != ss[0]) { attr = ss[0].split(':')[1]; }
                var val = s.split('[')[0];
                if (val != s)
                { val = s.split('[')[1].replace(/[",\]]/g, ''); }
                else { val = ''; }
                s = s.split('[')[0];
                var obj = []; var sObj = null;
                o = o || document;
                switch (s.charAt(0)) {
                    case '#':
                        sObj = document.getElementById(s.substr(1));
                        if (sObj) obj.push(sObj);
                        break;
                    case '.':
                        var l = o.getElementsByTagName('*');
                        var c = s.substr(1);
                        for (var i = 0; i < l.length; i++)
                            if (l[i].className.search('\\b' + c + '\\b') != -1) obj.push(l[i]);
                        break;
                    default:
                        obj = o.getElementsByTagName(s);
                        break;
                }
                if (val) {
                    var l = [];
                    var a = val.split('=');
                    for (var i = 0; i < obj.length; i++) {
                        if (a.length == 2 && obj[i][a[0]] == a[1]) { l.push(obj[i]) };
                    }
                    obj = l;
                }
                if (attr) {
                    var l = [];
                    for (var i = 0; i < obj.length; i++) {
                        if (obj[i][attr]) { l.push(obj[i]); }
                    }
                    obj = l;
                }

                if (ss.length > 1) {
                    var l = [];
                    for (var i = 0; i < obj.length; i++) {
                        var ll = arguments.callee(q.substr(ss[0].length + 1), obj[i]);
                        if (ll.tagName) { l.push(ll); }
                        else
                        { for (var j = 0; j < ll.length; j++) { l.push(ll[j]); } }
                    }
                    obj = l;
                }
                if (sObj && ss.length == 1) {
                    //当为ID选择器时，直接返回对象。
                    obj = sObj;
                    if (obj) { obj.length = 1; }
                } else {
                    //去除数组中重复元素
                    var l = [];
                    for (var i = 0; i < obj.length; i++)
                    { obj[i].$isAdd = false; }
                    for (var i = 0; i < obj.length; i++) {
                        if (!obj[i].$isAdd) {
                            obj[i].$isAdd = true;
                            l.push(obj[i]);
                        }
                    }
                    obj = l;
                }
                if (obj.length == 0) {
                    return null;
                }
                else if (obj.length == 1) {
                    return obj[0];
                }
                else {
                    return obj;
                }
            },
            close: function (e) {
                document.documentElement.removeChild(document.getElementById(that.config.id));
                if (that.config.shade.show) {
                    document.documentElement.removeChild(document.getElementById(that.config.id + "-shade"));
                }
                if (window["layergofunc" + that.config.id]) {
                    window["layergofunc" + that.config.id] = null;
                }
                if (that.config.endsuccess) {
                    that.config.endsuccess(that);
                }
            },
            parseNode: function (arg) { var objE = document.createElement("div"); objE.innerHTML = arg; return objE.childNodes[0]; },
            parseHTML: function (dom) { var objE = document.createElement("div"); objE.appendChild(dom); return objE.innerHTML; }
        }
        var that = this.o;
    };
    $layer.alert = function (parameter, icon, callback) {
        if (typeof (parameter) == "object") {
            parameter.dialog = { icon: icon ? icon : 3, success: callback };
            parameter.btns = { btn: ["确定"], count: 1 };
            parameter.area = { width: 230, height: 160 };
            return new $layer(parameter);
        }
        else {
            var p = { dialog: { icon: icon, msg: parameter, success: callback }, btns: { btn: ["确定"], count: 1 }, area: { width: 230, height: 160} };
            return new $layer(p);
        }
    }
    $layer.confirm = function (parameter, icon, callback) {
        if (typeof (parameter) == "object") {
            parameter.dialog = { icon: icon ? icon : 4, success: callback };
            parameter.btns = { btn: ["确定"], count: 1 };
            parameter.area = { width: 230, height: 160 };
            parameter.type = 1;
            return new $layer(parameter);
        }
        else {
            var p = { type: 1, dialog: { icon: icon ? icon : 4, msg: parameter, success: callback }, btns: { btn: ["确定", "取消"], count: 2 }, area: { width: 230, height: 160} };
            return new $layer(p);
        }
    }
    $layer.loading = function (parameter, icon, callback) {
        if (typeof (parameter) == "object") {
            parameter.dialog = { icon: icon ? icon : 4, success: callback };
            parameter.area = { width: 170, height: 30 };
            parameter.type = 2;
            return new $layer(parameter);
        }
        else {
            var p = { border: { show: false }, move: { ismove: false }, oframe: { head: false, body: true, foot: false }, type: 2, dialog: { icon: icon ? icon : 4, msg: parameter, success: callback }, area: { width: 170, height: 30} };
            return new $layer(p);
        }
    }
    $layer.page = function (parameter, callback) {
        if (typeof (parameter) == "object") {
            parameter.dialog = { success: callback };
            parameter.btns = { count: 0 };
            parameter.type = 3;
            return new $layer(parameter);
        }
        else {
            var p = { type: 3, dialog: { success: callback }, btns: { count: 0 }, page: { html: parameter} };
            return new $layer(p);
        }
    }
    $layer.win = function (parameter) {
        var p = {};
        p.title = parameter.title == null ? "无标题文档" : parameter.title;
        p.zclass = parameter.layerclass == null ? "layer" : "layer " + parameter.layerclass;
        p.type = 4;
        p.iframe = { success: parameter.success, src: parameter.url };
        p.btns = { count: 2 };
        p.page = { html: parameter.url };
        p.area = { width: parameter.width == null ? 550 : parameter.width, height: parameter.height == null ? 370 : parameter.height };
        return new $layer(p);
    }
    $layer.regist = function (layerid, fuc) {
        if (layerid && layerid.indexOf("http:") != -1) {
            layerid = arguments[0].substring(arguments[0].indexOf("layer-id"), arguments[0].length).split("&")[0].split("=")[1];
        }
        top.window["layerwinfuc" + layerid] = fuc;
        top.document.getElementById("layer-btn-ok-" + layerid).onclick = function () {
            fuc();
        }
    }
    $layer.close = function (layerid) {
        if (layerid && layerid.indexOf("http:") != -1) {
            layerid = arguments[0].substring(arguments[0].indexOf("layer-id"), arguments[0].length).split("&")[0].split("=")[1];
        }
        if (window["layergofunc" + layerid]) {
            window["layergofunc" + layerid] = null;
        }
        var obj = document.getElementById(layerid);
        if (obj) {
            document.documentElement.removeChild(obj);
        }
        if (document.getElementById(layerid + "-shade")) {
            document.documentElement.removeChild(document.getElementById(layerid + "-shade"));
        }
    }
    $layer.callback = function (layerid) {
        if (layerid && layerid.indexOf("http:") != -1) {
            layerid = arguments[0].substring(arguments[0].indexOf("layer-id"), arguments[0].length).split("&")[0].split("=")[1];
        }

        var callparameter = "";
        for (var i = 1; i < arguments.length; i++) {
            if (callparameter != "") {
                callparameter += ",";
            }
            callparameter += "arguments[" + i + "]";
        }
        if (window["layergofunc" + layerid]) {
            eval("window[\"layergofunc" + layerid + "\"](" + callparameter + ")");
        }
//        var temparguments = [].slice.apply(arguments);
//        var temparray = [];
//        if (temparguments.length > 1) {
//            temparray = temparguments.slice(1, temparguments.length);
//        }
//        if (window["layergofunc" + layerid]) {
//            window["layergofunc" + layerid](temparray);
//        }
    }
    var Common = {
        getEvent: function () {
            if (document.all) {
                return window.event;
            }
            func = getEvent.caller;
            while (func != null) {
                var arg0 = func.arguments[0];
                if (arg0) {
                    if ((arg0.constructor == Event || arg0.constructor == MouseEvent) || (typeof (arg0) == "object" && arg0.preventDefault && arg0.stopPropagation)) {
                        return arg0;
                    }
                }
                func = func.caller;
            }
            return null;
        },
        getMousePos: function (ev) {
            if (!ev) {
                ev = this.getEvent();
            }
            if (ev.pageX || ev.pageY) {
                return {
                    x: ev.pageX,
                    y: ev.pageY
                };
            }
            if (document.documentElement && document.documentElement.scrollTop) {
                return {
                    x: ev.clientX + document.documentElement.scrollLeft - document.documentElement.clientLeft,
                    y: ev.clientY + document.documentElement.scrollTop - document.documentElement.clientTop
                };
            }
            else if (document.body) {
                return {
                    x: ev.clientX + document.body.scrollLeft - document.body.clientLeft,
                    y: ev.clientY + document.body.scrollTop - document.body.clientTop
                };
            }
        },
        getItself: function (id) {
            return "string" == typeof id ? document.getElementById(id) : id;
        },
        getViewportSize: { w: (window.innerWidth) ? window.innerWidth : (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.offsetWidth, h: (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight },
        isIE: document.all ? true : false,
        setOuterHtml: function (obj, html) {
            var Objrange = document.createRange();
            obj.innerHTML = html;
            Objrange.selectNodeContents(obj);
            var frag = Objrange.extractContents();
            if (obj.parentNode) {
                obj.parentNode.insertBefore(frag, obj);
                obj.parentNode.removeChild(obj);
            }
        }
    };
    var Class = {
        create: function () {
            return function () { this.init.apply(this, arguments); }
        }
    };
    var Drag = Class.create();
    Drag.prototype = {
        init: function (titleBar, dragDiv, Options) {
            //设置点击是否透明，默认不透明
            titleBar = Common.getItself(titleBar);
            dragDiv = Common.getItself(dragDiv);
            this.dragArea = { maxLeft: 0, maxRight: Common.getViewportSize.w - dragDiv.offsetWidth - 2, maxTop: 0, maxBottom: Common.getViewportSize.h - dragDiv.offsetHeight - 2 };
            if (Options) {
                this.opacity = Options.opacity ? (isNaN(parseInt(Options.opacity)) ? 100 : parseInt(Options.opacity)) : 100;
                this.keepOrigin = Options.keepOrigin ? ((Options.keepOrigin == true || Options.keepOrigin == false) ? Options.keepOrigin : false) : false;
                if (this.keepOrigin) { this.opacity = 50; }
                if (Options.area) {
                    if (Options.area.left && !isNaN(parseInt(Options.area.left))) { this.dragArea.maxLeft = Options.area.left };
                    if (Options.area.right && !isNaN(parseInt(Options.area.right))) { this.dragArea.maxRight = Options.area.right };
                    if (Options.area.top && !isNaN(parseInt(Options.area.top))) { this.dragArea.maxTop = Options.area.top };
                    if (Options.area.bottom && !isNaN(parseInt(Options.area.bottom))) { this.dragArea.maxBottom = Options.area.bottom };
                }
            }
            else {
                this.opacity = 100, this.keepOrigin = false;
            }
            this.originDragDiv = null;
            this.tmpX = 0;
            this.tmpY = 0;
            this.moveable = false;
            var dragObj = this;
            titleBar.onmousedown = function (e) {
                var ev = e || window.event || Common.getEvent();
                //只允许通过鼠标左键进行拖拽,IE鼠标左键为1 FireFox为0
                if (Common.isIE && ev.button == 0 || Common.isIE && ev.button == 1 || !Common.isIE && ev.button == 0) {
                }
                else {
                    return false;
                }
                if (dragObj.keepOrigin) {
                    dragObj.originDragDiv = document.createElement("div");
                    dragObj.originDragDiv.style.cssText = dragDiv.style.cssText;
                    dragObj.originDragDiv.style.width = dragDiv.offsetWidth;
                    dragObj.originDragDiv.style.height = dragDiv.offsetHeight;
                    //dragObj.originDragDiv.innerHTML = dragDiv.innerHTML;
                    dragObj.originDragDiv.innerHTML = "";
                    dragDiv.parentNode.appendChild(dragObj.originDragDiv);
                }
                dragObj.moveable = true;
                dragDiv.style.zIndex = dragObj.GetZindex() + 1;
                var downPos = Common.getMousePos(ev);
                dragObj.tmpX = downPos.x - dragDiv.offsetLeft;
                dragObj.tmpY = downPos.y - dragDiv.offsetTop;

                titleBar.style.cursor = "move";
                if (Common.isIE) {
                    dragDiv.setCapture();
                } else {
                    window.captureEvents(Event.mousemove);
                }
                dragObj.SetOpacity(dragDiv, dragObj.opacity);
                //FireFox 去除容器内拖拽图片问题
                if (ev.preventDefault) {
                    ev.preventDefault();
                    ev.stopPropagation();
                }
                document.onmousemove = function (e) {
                    if (dragObj.moveable) {
                        var ev = e || window.event || Common.getEvent();
                        //IE 去除容器内拖拽图片问题
                        if (document.all) //IE
                        {
                            ev.returnValue = false;
                        }
                        var movePos = Common.getMousePos(ev);
                        dragDiv.style.left = Math.max(Math.min(movePos.x - dragObj.tmpX, dragObj.dragArea.maxRight), dragObj.dragArea.maxLeft) + "px";
                        dragDiv.style.top = Math.max(Math.min(movePos.y - dragObj.tmpY, dragObj.dragArea.maxBottom), dragObj.dragArea.maxTop) + "px";

                    }
                };
                document.onmouseup = function () {
                    if (dragObj.keepOrigin) {
                        if (Common.isIE && dragObj.originDragDiv.outerHTML) {
                            dragObj.originDragDiv.outerHTML = "";
                        }
                        else {
                            Common.setOuterHtml(dragObj.originDragDiv, "");
                        }
                    }
                    if (dragObj.moveable) {
                        if (Common.isIE) {
                            dragDiv.releaseCapture();
                        }
                        else {
                            window.releaseEvents(dragDiv.mousemove);
                        }
                        dragObj.SetOpacity(dragDiv, 100);
                        titleBar.style.cursor = "default";
                        dragObj.moveable = false;
                        dragObj.tmpX = 0;
                        dragObj.tmpY = 0;
                    }
                };
            }
        },
        SetOpacity: function (dragDiv, n) {
            if (Common.isIE) {
                if (dragDiv.filters && dragDiv.filters.alpha) {
                    dragDiv.filters.alpha.opacity = n;
                }
            }
            else {
                dragDiv.style.opacity = n / 100;
            }
        },
        GetZindex: function () {
            var maxZindex = 0;
            var divs = document.getElementsByTagName("div");
            for (z = 0; z < divs.length; z++) {
                maxZindex = Math.max(maxZindex, divs[z].style.zIndex);
            }
            return maxZindex;
        }
    };
})();